package com.laoxu.student.business.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.laoxu.student.business.entity.Major;
import com.laoxu.student.business.service.MajorService;
import com.laoxu.student.business.vo.MajorVO;
import com.laoxu.student.common.vo.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  专业控制器
 * </p>
 *
 * @author Laoxu
 * @since 2020-04-04
 */
@Api(description="专业管理")
@CrossOrigin //跨域
@RestController
@RequestMapping("/major")
public class MajorController {
    @Autowired
    private MajorService majorService;
    
    @ApiOperation("新增或修改专业")
    @PostMapping("save")
    public Result save(@ApiParam(name="major",value = "专业对象",required = true)
                       @RequestBody Major major){
        majorService.saveOrUpdate(major);
        return Result.ok().data("majorId",major.getId());
    }

    @ApiOperation("根据ID查询专业")
    @GetMapping("{id}")
    public Result get(@ApiParam(name="id",value = "专业ID",required = true)
                      @PathVariable Long id){
        Major major = majorService.getById(id);
        return Result.ok().data("item", major);
    }

    @ApiOperation("删除多个专业")
    @PostMapping("removes")
    public Result removes(@ApiParam(name="ids",value = "专业ID数组",required = true)
                          @RequestBody List<Long> ids){
        majorService.removeByIds(ids);
        return Result.ok();
    }

    @ApiOperation(value = "专业分页列表")
    @GetMapping("{page}/{limit}")
    public Result pageQuery(
            @ApiParam(name = "page", value = "当前页码", required = true)
            @PathVariable Long page,
            @ApiParam(name = "limit", value = "每页记录数", required = true)
            @PathVariable Long limit){

        Page<MajorVO> pageParam = new Page<>(page, limit);
        majorService.pageQuery(pageParam);

        return  Result.ok().data("total", pageParam.getTotal()).data("rows", pageParam.getRecords());
    }

    @ApiOperation(value = "专业列表")
    @GetMapping("list")
    public Result list(@ApiParam(name = "deptId", value = "院系ID", required = false)
                           @RequestParam(required = false) Long deptId){
        QueryWrapper<Major> queryWrapper = new QueryWrapper<>();
        if(deptId!=null){
            queryWrapper.eq("dept_id",deptId);
        }
        List<Major> records = majorService.list(queryWrapper);

        return  Result.ok().data("items", records);
    }
}

